﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSharp.Libs.Koc;
using CSharp.Libs.Koc.Models;
using System.Collections;

namespace KocKernelControl
{
    /// <summary>
    /// Interaktionslogik für UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        #region declarations
        private Kernel _KRNL = new Kernel();

        private string _strFbUids = "";
        private string _strSeedInfo = "";

        private const string DASH_LINE = "-------------------------------";
        #endregion



        #region constructor / terminator
        public UserControl1()
        {
            InitializeComponent();

            _KRNL.SC.SaveSeedToDisk = true;

            _KRNL.TM.ReceivingEventsChanged += new TrafficMonitor.ReceivingEventsChangedEventHandler(TM_ReceivingEventsChanged);
            _KRNL.TM.ChatReceived += new TrafficMonitor.ChatReceivedEventHandler(TM_ChatReceived);
            _KRNL.SC.SeedReceived += new SeedCollector.SeedReceivedEventHandler(SC_SeedReceived);
        }
        #endregion



        #region event handling
        void TM_ReceivingEventsChanged(object sender)
        {
            this.Dispatcher.Invoke((Action)delegate
            {
                txtReceiving.Text = ((_KRNL.TM.ReceivingEvents) ? "X" : "");
            });
        }

        void TM_ChatReceived(object sender, CSharp.Libs.Koc.KernelModels.ChatReceivedEventArgs crea)
        {

            string strFormat = "{0,-20} {1,-10} {2,-20}";
            _strFbUids = string.Concat(Environment.NewLine, Environment.NewLine,
                "Facebook UID Collection", Environment.NewLine, DASH_LINE, Environment.NewLine);
            _strFbUids = string.Concat(_strFbUids, string.Format(strFormat, "FbUid", "Valid", "Last Checked Timestamp"));
            _strFbUids = string.Concat(_strFbUids, Environment.NewLine);

            if (PhpCommandGenerator.Users.Count > 0)
            {
                foreach (KeyValuePair<string, UserValidatorItem> KVP in PhpCommandGenerator.Users)
                {
                    UserValidatorItem UVI = KVP.Value;

                    _strFbUids = string.Concat(_strFbUids,
                        string.Format(strFormat, UVI.FbUid, UVI.UserIsValid.ToString(), UVI.LastCheckedTimestamp.ToString()),
                        Environment.NewLine);
                }
            }

            _showResultText();
        }

        void SC_SeedReceived(object sender, CSharp.Libs.Koc.KernelModels.SeedReceivedEventArgs srea)
        {
            string strFormat = "{0,-20} {1,-12} {2,-20}";
            _strSeedInfo = string.Concat(Environment.NewLine, Environment.NewLine,
                "Seed Collector Results", Environment.NewLine, DASH_LINE, Environment.NewLine);

            if (_KRNL.SC.Seeds.Count > 0)
            {
                foreach (KeyValuePair<string, object> KVP in _KRNL.SC.Seeds)
                {
                    Hashtable htSeed = KVP.Value as Hashtable;

                    if (htSeed != null)
                    {
                        int intPrevtick = -1;
                        int.TryParse(htSeed["prevtick"].ToString(), out intPrevtick);

                        _strSeedInfo = string.Concat(_strSeedInfo, string.Format(strFormat, "SeedKey", "Timestamp", "Datum/Uhrzeit"));
                        _strSeedInfo = string.Concat(_strSeedInfo, Environment.NewLine);
                        _strSeedInfo = string.Concat(_strSeedInfo,
                            string.Format(strFormat, KVP.Key, intPrevtick.ToString(), Converters.Timestamp2Date(intPrevtick).ToString()),
                            Environment.NewLine);

                        _addPlayerData(htSeed);
                        _addCityData(htSeed);
                    }
                }

                _showResultText();
            }
        }
        #endregion



        #region private functions
        private void _addCityData(
            Hashtable htSeed)
        {
            if (htSeed != null)
            {
                ArrayList aryCities = htSeed[constants.KEY_SEED_CITIES] as ArrayList;

                if (aryCities != null)
                {
                    string strFormat = "    {0,-20} {1,-20} {2,-20} {3,-20} {4,-20}";
                    string strCityInfo = "";
                    strCityInfo = string.Concat(Environment.NewLine, "    CityInfo:", Environment.NewLine);
                    strCityInfo = string.Concat(strCityInfo, string.Format(strFormat, "CityID", "Name", "X,Y", "Unknown1", "Unknown2"));
                    strCityInfo = string.Concat(strCityInfo, Environment.NewLine);

                    foreach (ArrayList aryCity in aryCities)
                    {
                        strCityInfo = string.Concat(
                            strCityInfo,
                            string.Format(strFormat,
                            aryCity[SeedCityData.CityID.GetHashCode()].ToString(),
                            aryCity[SeedCityData.Name.GetHashCode()].ToString(),
                            string.Concat(aryCity[SeedCityData.XCoord.GetHashCode()].ToString(), ",", aryCity[SeedCityData.YCoord.GetHashCode()].ToString()),
                            aryCity[SeedCityData.Unknown1.GetHashCode()].ToString(),
                            aryCity[SeedCityData.Unknown2.GetHashCode()].ToString()),
                            Environment.NewLine);

                    }

                    _strSeedInfo = string.Concat(_strSeedInfo, strCityInfo, Environment.NewLine);
                }
            }
        }

        private void _addPlayerData(
            Hashtable htSeed)
        {
            if (htSeed != null)
            {
                Hashtable htPlayer = htSeed[constants.KEY_SEED_PLAYER] as Hashtable;
                Hashtable htPlayers = htSeed[constants.KEY_SEED_PLAYERS] as Hashtable;
                Hashtable htAllianceNames = htSeed[constants.KEY_SEED_ALLIANCE_NAMES] as Hashtable;
                Hashtable htTutorial = htSeed[constants.KEY_SEED_TUTORIAL] as Hashtable;

                if (htPlayer != null && htPlayers != null && htAllianceNames != null && htTutorial != null)
                {
                    string strPlayerFullName = string.Concat(
                        htPlayer[constants.KEY_SEED_PLAYER_FNAME].ToString(),
                        " ",
                        htPlayer[constants.KEY_SEED_PLAYER_LNAME].ToString());
                    string strName = "";
                    string strMight = "";
                    string strAlliance = "";

                    string strUserId = htTutorial[constants.KEY_USER_ID].ToString();
                    

                    foreach (DictionaryEntry dePlayer in htPlayers)
                    {
                        Hashtable htFirstPlayer = dePlayer.Value as Hashtable;
                        if (htFirstPlayer != null && dePlayer.Key.ToString().Substring(1) == strUserId)
                        {
                            strName = htFirstPlayer[constants.KEY_N].ToString();
                            strMight = htFirstPlayer[constants.KEY_M].ToString();
                            string strAllianceName = "";
                            string strAllianceKey = "";
                            foreach (DictionaryEntry deAllianceName in htAllianceNames)
                            {
                                strAllianceKey = deAllianceName.Key.ToString();
                                strAllianceName = deAllianceName.Value.ToString();
                                break;
                            }

                            if (strAllianceKey != "" &&
                                strAllianceKey.Substring(1) == htFirstPlayer[constants.KEY_A].ToString())
                            {
                                strAlliance = strAllianceName;
                            }

                            break;
                        }
                    }

                    string strFormat = "    {0,-20} {1,-20} {2,-20} {3,-20}";
                    string strPlayerInfo = "";
                    strPlayerInfo = string.Concat(Environment.NewLine, "    PlayerInfo:", Environment.NewLine);
                    strPlayerInfo = string.Concat(strPlayerInfo,
                        string.Format(strFormat, "Name", "Might", "Alliance", "Realname"),
                        Environment.NewLine);
                    strPlayerInfo = string.Concat(strPlayerInfo,
                        string.Format(strFormat, strName, strMight, strAlliance, strPlayerFullName),
                        Environment.NewLine);

                    _strSeedInfo = string.Concat(_strSeedInfo, strPlayerInfo, Environment.NewLine);
                }
            }
        }

        private void _showResultText()
        {
            string strResult = string.Concat(
                _strFbUids, Environment.NewLine,
                _strSeedInfo, Environment.NewLine);

            this.Dispatcher.Invoke((Action)delegate
            {
                txtResult.Text = strResult;
            });
        }
        #endregion



        #region interface
        #region properties
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
